package com.yanqu.road.dao.impl.cross.unionwar;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.unionwar.UnionWarRankMember;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author ：zfr
 * @description：商会可领奖成员Dao
 * @date ：2021/1/26 16:44
 */
public class UnionWarRankMemberDaoImpl extends TempDataObjectDao<UnionWarRankMember> {
    @Override
    protected boolean addData(UnionWarRankMember data) {
        String sql = "insert into t_u_activity_unionwar_rank_member(activityId,phase,unionUid,userId,positionId) values (?,?,?,?,?)";
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, data.getActivityId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getPhase()));
        para.put(i++, new DbParameter(Types.VARCHAR, data.getUnionUid()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getPositionId()));
        if (getDb().executeNoneQuery(sql, para) > 0) {
            return true;
        }
        return false;
    }

    @Override
    protected boolean updateData(UnionWarRankMember data) {
        return false;
    }

    @Override
    protected UnionWarRankMember parseData(ResultSet rs) throws SQLException {
        UnionWarRankMember data = new UnionWarRankMember();
        data.setActivityId(rs.getInt("activityId"));
        data.setPhase(rs.getInt("phase"));
        data.setUnionUid(rs.getString("unionUid"));
        data.setUserId(rs.getLong("userId"));
        data.setPositionId(rs.getInt("positionId"));
        return data;
    }

    public Map<String, Map<Integer, Map<Long, UnionWarRankMember>>> getUnionWarRankMemberMap(int activityId) {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<String, Map<Integer, Map<Long, UnionWarRankMember>>> dataMap = new ConcurrentHashMap<>();
        String sql = "select * from t_u_activity_unionwar_rank_member where activityId = ?";
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(sql);
            int i = 1;
            call.setInt(i++,activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                UnionWarRankMember data = parse(rs);
                if(!dataMap.containsKey(data.getUnionUid())){
                    dataMap.put(data.getUnionUid(), new ConcurrentHashMap<>());
                }
                if(!dataMap.get(data.getUnionUid()).containsKey(data.getPhase())){
                    dataMap.get(data.getUnionUid()).put(data.getPhase(), new ConcurrentHashMap<>());
                }
                dataMap.get(data.getUnionUid()).get(data.getPhase()).put(data.getUserId(), data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return dataMap;
    }


}
